package org.example.springboot.service;

import org.example.springboot.entity.Need;
import org.example.springboot.entity.UserAndNeed;
import org.example.springboot.mapper.NeedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NeedService {

    @Autowired
    NeedMapper Mapper;

    public List<Need> findByNameAndCourse(String name, String course, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return Mapper.findByNameAndCourse(name, course, offset, pageSize);
    }


    public List<Need> findByName(String name, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return Mapper.findByName(name, offset, pageSize);
    }


    public List<Need> findByCourse(String course, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return Mapper.findByCourse(course, offset, pageSize);
    }


    public int countByNameAndCourse(String name, String course) {
        return Mapper.countByNameAndCourse(name, course);
    }


    public int countByName(String name) {
        return Mapper.countByName(name);
    }


    public int countByCourse(String course) {
        return Mapper.countByCourse(course);
    }

    public List<UserAndNeed> findUserAndNeed(String course) {
        return Mapper.selectByCourse(course);
    }

    public UserAndNeed findNeedByAccountAndCourse(String account, String course) {
        return Mapper.findNeedByAccountAndCourse(account, course);
    }

    public List<UserAndNeed> findAllNeed() {
        return Mapper.findAllNeed();
    }

    public List<Need> findNeedByAccount(String account) {
        return Mapper.findNeedByAccount(account);
    }

    @Transactional
    public void insertNeed(Need need){
        Mapper.insertNeed(need);
    }

    @Transactional
    public void deleteNeed(int id) {
        Mapper.deleteNeed(id);
    }
}
